﻿@using Newtouch.Infrastructure;
@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Index.cshtml";
}
<form name="form1">
    <div class="panel panel-default" style="margin-bottom: 0%;">
        <div class="panel-heading navb-bg">
            筛选条件
        </div>
        <div style="padding-top:1%;padding-bottom:1%; padding-right:7%;" id="divInpatientBasicInfo">
            <table class="form">
                <tr>
                    <th class="formTitle">住院号：</th>
                    <td class="formValue">
                        <input type="text" class="form-control" id="zyh" style="width:49%;float:left;" />
                        &nbsp;&nbsp;<input type="button" class="btn btn-default btn-md btn-default-color" title="选择住院病人" style="width: 25%;" id="btnsyy" value="查询" onclick="GetPatSerarchView($('#zyh').val());">
                    </td>
                    <th class="formTitle">记账日期：</th>
                    <td class="formValue" colspan="3">
                        <input id="kssj" type="text" class="form-control input-wdatepicker formClearIgnore" style="width:29%; float:left;" value="@DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd")" onfocus="WdatePicker({ dateFmt: 'yyyy-MM-dd' })" />
                        <span style="margin-left:5%;float:left;">—</span>
                        <input id="jssj" type="text" class="form-control input-wdatepicker formClearIgnore" style="width :29%;float:left; margin-left:5%;" value="@DateTime.Now.ToString("yyyy-MM-dd")" onfocus="WdatePicker({ dateFmt: 'yyyy-MM-dd' })" />
                    </td>
                    @*<td class="formValue">
                        <input type="button" id="btn_search" class="btn btn-primary" style="width:40%;margin-left:10%;" value="查询" />
                    </td>*@
                </tr>
                <tr>
                    <th class="formTitle">项目类别：</th>
                    <td class="formValue">
                        <input type="text" class="form-control" id="xmlb" name="xmlb" style="width:90%;float:left;" placeholder="最少1位字符" />
                    </td>
                    <th class="formTitle">项目名称：</th>
                    <td class="formValue">
                        <input type="text" class="form-control" id="xmmc" style="width:90%;float:left;" />
                    </td>
                    <td class="formValue">
                        <input type="button" id="btn_search" class="btn btn-primary" style="width:40%;margin-left:25%;" value="查询" />
                    </td>
                </tr>
            </table>
        </div>
    </div>
    <div class="panel panel-default" style="margin-bottom: 1%;" id="divPatInfo">
        <table class="form">
            <tr>
                <th class="formTitle">姓名：</th>
                <td class="formValue">
                    <label id="xm"></label>
                </td>
                <th class="formTitle">性别：</th>
                <td class="formValue">
                    <label id="xb"></label>
                </td>
                <th class="formTitle">年龄：</th>
                <td class="formValue">
                    <label id="nlshow"></label>
                </td>
                <th class="formTitle">证件号：</th>
                <td class="formValue" colspan="3">
                    <label id="zjh"></label>
                </td>
            </tr>
            <tbody class="dispTbody" style="display:none;">
                <tr>
                    <th class="formTitle">入院诊断：</th>
                    <td class="formValue">
                        <label id="ryzdmc"></label>
                    </td>
                    <th class="formTitle">在院状态：</th>
                    <td class="formValue">
                        <label id="zybz"></label>
                    </td>
                    <th class="formTitle">报销政策：</th>
                    <td class="formValue">
                        <label id="brxzmc"></label>
                    </td>
                    <th class="formTitle">出生日期：</th>
                    <td class="formValue">
                        <label id="csny"></label>
                    </td>
                    <th class="formTitle">入院日期：</th>
                    <td class="formValue">
                        <label id="ryrq"></label>
                    </td>
                </tr>
            </tbody>
        </table>
        <div class="fa_icontoggle"><i class="fa fa-angle-double-down icontoggle" style="color:rgb(0, 160, 233);cursor:pointer;"></i></div>
    </div>
    <div class="gridPanel" style="margin-top:1%">
        <table id="gridList"></table>
        <div id="gridPager"></div>
    </div>
</form>
@Html.Partial("_BottomButtonsView", new Newtouch.HIS.Web.Core.Models.BottomButtonViewModel()
{
    ShowKeyList = new int[] { 4, 6 },
    F6Text = "退费"
})
<script>
    var from = $.request("from");
    var zfy = 0.0000;
    $(function () {

        $('#xmlb').newtouchBatchFloatingSelector({
            width: 250,
            height: 200,
            caption: "选择类别",
            clickautotrigger: true,
            url: "/SystemManage/SysChargeCategory/GetsfdlJson",
            ajaxparameters: function ($thisinput) {
                var keyword = $thisinput.val().trim();
                return "keyword=" + keyword;
            },
            itemdbclickhandler: function ($thistr, $thisinput) {
                $('#xmlb').val($thistr.attr('data-dlmc'))
            },
            colModel: [
                { label: '名称', name: 'dlmc', width: 150, align: 'center' },
                { label: '拼音', name: 'py', width: 50, align: 'center' },
                { label: 'dlCode', name: 'dlCode', hidden: true, width: 100, align: 'center' }
            ]
        });
    });
    //查询
    $('#btn_search').click(function() {
        if ($('#kssj').val() && $('#jssj').val() && ($('#kssj').val() > $('#jssj').val())) {
            $.modalAlert("开始日期不能大于结束日期", 'warning');
            return;
        }
        ajaxLoadDataResult();
    });
    //回车查询
    $('#zyh').keydownEnterEvent(function () {
        ajaxLoadDataResult();
    });

    //住院病人查询
    function GetPatSerarchView(zyh) {
        if (!!!zyh) {
            zyh = '';
        }
        $.modalOpen({
            id: "patSearch",
            title: "患者查询",
            url: "/PatientManage/AccountManage/PatSearchView?brzybzType=" + '@((int)EnumZYBZ.Xry + "," + (int)EnumZYBZ.Bqz + "," + (int)EnumZYBZ.Djz)' + "&t=" + Math.random() + "&zyh=" + zyh,
            width: "700px",
            height: "600px",
            callBack: function (iframeId) {
                top.frames[iframeId].PatDbGrid(); //在弹出窗口事件
            }//窗口点确定的回调函数
        });
    }

    //选择的病人
    function getPatInfoAjax(selePatInfo) {
        $('#zyh').val(selePatInfo.zyh);
        $("#btn_search").trigger('click');
    }
    //var arraylist = new Array();
    //ajax加载数据
    function ajaxLoadDataResult() {
        //清空Grid
        $('#gridList').jqGrid("clearGridData");

        var zyh = $.trim($("#zyh").val());
        if (zyh == '') {
            $.modalAlert("请输入住院号。", 'warning');
            return false;
        }
        var xmlb = $.trim($("#xmlb").val());
        var xmmc = $.trim($("#xmmc").val());

        $.najax({
            type: "POST",
            url: "/HospitalizationManage/InpatientRefund/InpatientRefundQuery",
            data: { zyh: zyh, kssj: $('#kssj').val(), jssj: $('#jssj').val(), xmlb: xmlb, xmmc: xmmc, conf: from },
            dataType: "json",
            //async: false,
            success: function (ajaxresp) {
                if (!ajaxresp.data) {
                    $.modalAlert("未查询到该病人信息,请确认。", 'warning');
                    return;
                }
                //住院病人信息
                $('#divPatInfo').formSerialize(ajaxresp.data.InpatientSettPatInfo, true);
                $('#nlshow').html(getAgeFromBirthTime({ begin: ajaxresp.data.InpatientSettPatInfo.csny }).text);
                $('#csny').text((ajaxresp.data.InpatientSettPatInfo.csny && ajaxresp.data.InpatientSettPatInfo.csny.length >= 10 ? ajaxresp.data.InpatientSettPatInfo.csny.substring(0, 10) : ""))
                $("#xb").text($.getGender(ajaxresp.data.InpatientSettPatInfo.xb));
                $("#zybz").text(ajaxresp.data.InpatientSettPatInfo.zybz == '0' ? '入院登记' : (ajaxresp.data.InpatientSettPatInfo.zybz == '1' ? '病区中' : '其他'));
                $("#ryrq").text((ajaxresp.data.InpatientSettPatInfo.ryrq && ajaxresp.data.InpatientSettPatInfo.ryrq.length >= 10 ? ajaxresp.data.InpatientSettPatInfo.ryrq.substring(0, 10) : ""));

                //计费明细、分页；总金额合计
                //拼接药品和项目，重新排序集合
                //var c = ajaxresp.data.InpatientSettleItemBO.TreatmentItemList.concat(ajaxresp.data.InpatientSettleItemBO.DrugList).sort(function (a, b) { return new Date(a.tdrq) - new Date(b.tdrq); });
                var c = ajaxresp.data.InpatientSettleItemBO.TreatmentItemList.sort(function (a, b) { return new Date(a.tdrq) - new Date(b.tdrq); });
                //console.info(c);
                //arraylist = c;
                //$("#gridList").jqGrid('setGridParam', {
                //    data: arraylist,
                //    datatype: 'local'
                //}).trigger("reloadGrid");
                $("#gridList").newtouchLocalDataGrid(null, c);
                zfy = 0.00;
                $.each(c, function () {
                    zfy += this.je;
                });
                $("#gridList").jqGrid("setCaption", "计费明细（总金额：" + "<label style='color:red;'>￥"
                    + zfy.toFixed(2) + "</label>）");
            },
            errorCallback: function (data) {
                newtouch_globalevent_f4();
            }
        });
    }

    window.newtouch_globalconfig.f4opions = {
        container: "#divInpatientBasicInfo,#divPatInfo"
    };

    function newtouch_event_f4() {
        $('#gridList').jqGrid("clearGridData");
    }


    //计费明细
    $('#gridList').newtouchLocalDataGrid({
        caption: '计费明细',
        height: $(window).height() - 280,
        unwritten: false,
        editurl: "clientArray",
        //rownumbers: true,
        colModel: [
            
            { label: "isYP", name: "isYP", width: 100, align: "left", hidden: true },
            { label: "计费编码", name: "jfbbh", width: 100, align: "center", hidden: true },
            { label: "jfbbhs", name: "jfbbhs", width: 100, align: "center", hidden: true },
            { label: "dcztbs", name: "dcztbs", width: 100, align: "center", hidden: true },
            { label: "项目类别", name: "dlmc", width: 130, align: "left" },
            { label: "项目名称", name: "sfxmmc", width: 130, align: "left" },
            { label: "单位", name: "dw", width: 100, align: "left" },
            { label: "原来数量", name: "ylsl", width: 130, align: "left" },
            { label: "数量", name: "sl", width: 100, align: "left" },
            {
                label: "单价", name: "dj", width: 100, align: "left",
                formatter: function (val) {
                    if (val) {
                        return val.toFixed(4);
                    } else {
                        return "0.0000"
                    }
                }
            },
            {
                label: "金额", name: "je", width: 100, align: "left",
                formatter: function (val) {
                    if (val) {
                        return val.toFixed(2);
                    } else {
                        return "0.00"
                    }
                }
            },
            { label: "退数量", name: "tsl", width: 100, editable: true, editrules: { numeric: true, minValue: 0.5 }, align: "left" },
            { label: "退金额", name: "tje", width: 25, align: "left", hidden: true },
            { label: "性质", name: "zfxz", width: 25, align: "left", hidden: true },
            { label: "组套编号", name: "ztbh", width: 25, align: "left", hidden: true },
            { label: "ztsl", name: "ztsl", width: 25, align: "left", hidden: true },
            {
                label: "记账日期", name: "tdrq", width: 100, align: "left",
                formatter: "date", formatoptions: { srcformat: 'Y-m-d', newformat: 'Y-m-d' }
            }
        ],
        multiselect: true,
        gridComplete: function () {
           
            //启用行内编辑框
            EnableInlineEditBox();
        },
        onSelectRow: function (rowid) {
            $("#" + $.jgrid.jqID(rowid) + "_tsl").focus();
        }
        
    });

    //启用行内编辑框
    function EnableInlineEditBox() {
        //Grid加载完成开启行内编辑   1.先获取所有行Id  2.遍历调用editRow方法
        var rowIds = $("#gridList").jqGrid('getDataIDs');
        for (var i = 0; i < rowIds.length; i++) {
            $("#gridList").jqGrid('editRow', rowIds[i], true, null, null, null, null, function (callbackRowId) {     //true: when set to true we can use [Enter] key to save the row and [Esc] to cancel editing.

            });

        }
    }

    //退费确认
    function newtouch_event_f6() {
        //获取选中行Id
        var selRowIds = $("#gridList").jqGrid('getGridParam', 'selarrrow');
        if (selRowIds.length == 0) {
            $.modalAlert("请先选中需退费的信息", 'warning');
            return;
        }
        //获取所有行Id，遍历使编辑框处于保存状态
        var rowIds = $("#gridList").jqGrid('getDataIDs');
        for (var i = 0; i < rowIds.length; i++) {
            $("#gridList").saveRow(rowIds[i], null, null, null, function (callbackRowId) { }, null, null);
        }
        //判断退数量
        for (var i = 0; i < selRowIds.length; i++) {
            var rowData = $("#gridList").jqGrid('getRowData', selRowIds[i]);
            if (!rowData) {
                break;
            }
            if (rowData.tsl.replace(/(^\s*)|(\s*$)/g, "") == "") {
                $.modalAlert("项目（" + rowData.sfxmmc + "）退数量为空，请确认。", 'warning');
                //启用行内编辑框
                EnableInlineEditBox();
                return;
            }
            if (rowData.tsl <= 0 || isNaN(rowData.tsl)) {
                //启用行内编辑框
                EnableInlineEditBox();
                return;
            }
            if (parseFloat(rowData.tsl) > parseFloat(rowData.sl)) {
                $.modalAlert("项目（" + rowData.sfxmmc + "）退数量大于记账数量，请确认。", 'warning');
                //启用行内编辑框
                EnableInlineEditBox();
                return;
            }
        }

        //true:选中行JSON.stringify(ajaxresp.data)
        var data = $("#gridList").jqGrid('getRowData_AllLine', true, false);
        //setItem/getItem
        localStorage.setItem("needRefundData", data);
        //弹出退费确认窗体
        $.modalOpen({
            id: "FormShowRefundConfirmDialog",
            title: "退费明细确认",
            url: "/HospitalizationManage/InpatientRefund/RefundDetailConfirmForm",
            width: "500px",
            height: "450px",
            callBack: function (iframeId) {
                RefundMethod();
            },
            //弹窗点“X”事件和'关闭按钮'事件
            cancelCallBack: function (iframeId) {

                //启用行内编辑框
                EnableInlineEditBox();
            }
        });
    }

    //具体保存执行
    function RefundMethod() {
        //获取所有选中行数据
        var needRefundData = $("#gridList").jqGrid('getRowData_AllLine', true);
        for (var i = 0; i < needRefundData.length; i++) {
            var je = roundingBy4she6ru5chengshuang((parseFloat(needRefundData[i].dj) * parseFloat(needRefundData[i].tsl)), 2);
            //给Grid里的字段退金额赋值
            $('#gridList').jqGrid('setCell', needRefundData[i].jqRowId, 'tje', je);
        }
        var lastdata = $("#gridList").jqGrid('getRowData_AllLine', true, false);
        $.najax({
            url: "/HospitalizationManage/InpatientRefund/SaveRefund",
            dataType: "json",
            data: { data: lastdata, zyh: $.trim($("#zyh").val()) },
            type: "POST",
            success: function (data) {
                top.top.window.frames['FormShowRefundConfirmDialog'].$.modalClose();
                $.modalAlert("保存成功", 'success');
                //清空Grid
                $('#gridList').jqGrid("clearGridData");
                $("#btn_search").trigger('click');
            }
        });
    }

</script>
